package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
	"math/rand"
	"time"
)

func main() {
	c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", "127.0.0.12.9", 6379))
	if err != nil {
		panic(err)
	}
	defer (func() {
		fmt.Println("connection close")
		c.Close()
	})()

	if false {
		c.Send("del", "set", "list", "zset")
		c.Send("sadd", "set", "mark", "alen", "king")
		c.Send("lpush", "list", 10001, 10002, 10003)
		c.Send("smembers", "set")
		c.Send("lrange", "list", 0, -1)
		c.Flush()
		c.Receive() // del
		c.Receive() // sadd
		c.Receive() // lpush
		mbrs, err := redis.Strings(c.Receive())
		if err != redis.ErrNil {
			fmt.Println(mbrs)
		}
		lsts, err := redis.Ints(c.Receive())
		if err != redis.ErrNil {
			fmt.Println(lsts)
		}
	}

	if false {
		c.Send("del", "set", "list", "zset")
		c.Send("sadd", "set", "mark", "darren", "king")
		c.Send("lpush", "list", 10001, 10002, 10003)
		c.Do("")
	}

	if false {
		rand.Seed(time.Now().UnixNano())
		c.Send("del", "set", "list", "zset")
		c.Send("sadd", "set", "mark", "alen", "bob")
		{
			args := redis.Args{}.Add("zset")
			args = args.Add(rand.Intn(100)).Add("mark")
			args = args.Add(rand.Intn(100)).Add("alen")
			args = args.Add(rand.Intn(100)).Add("bob")
			c.Send("zadd", args...)
		}
		{
			args := redis.Args{}.Add("zset")
			args = args.Add(0).Add(-1).Add("withscores")
			vals, err := redis.Values(c.Do("zrange", args...))
			if err != nil {
				panic(err)
			}
			var rets []struct {
				Name  string
				Score int
			}
			if err = redis.ScanSlice(vals, &rets); err != nil {
				panic(err)
			}
			fmt.Println(rets)
		}
	}

}
